package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Places;
import es.ats.codington.festival.services.EventDAOService;
import es.ats.codington.festival.services.PlaceDAOService;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class EventRegistrationServlet
 */
@WebServlet("/EventRegistrationServlet")
public class EventRegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EventRegistrationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		EventDAOService eventDAO=new EventDAOService();
		
		response.setContentType("text/html");
		
		
		String eventName=request.getParameter("eventName");
		String eventDescrip=request.getParameter("eventDescrip");
		int placeId=Integer.parseInt(request.getParameter("place"));
		String duration=request.getParameter("duration");
		String eventType=request.getParameter("eventType");
		String ticket=request.getParameter("ticket");
		
		PlaceDAOService placeDAOService=new PlaceDAOService();
		
		Places place;
		place=placeDAOService.getPlaceByIdService(placeId);
		
		Event event=new Event(eventName, eventDescrip,duration,eventType,ticket,place);
		
		int i=eventDAO.insertEventService(event);
		if(i==1){
			RequestDispatcher rd= request.getRequestDispatcher("index2.jsp");
			rd.forward(request, response);
		}
		else {
			PrintWriter out=response.getWriter();
			out.print("Can not register this event.That event already exists.");
			RequestDispatcher rd= request.getRequestDispatcher("eventRegistration.jsp");
			rd.include(request, response);
			out.close();
		}
	
		
	}

}
